package com.cskaoyan.controller;


import com.cskaoyan.bean.bo.WxCouponExchangeBO;
import com.cskaoyan.bean.bo.WxCouponReceiveBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.vo.WxCouponListInnerVO;
import com.cskaoyan.bean.vo.WxCouponSelectlistInnerVO;
import com.cskaoyan.bean.vo.WxMylistInnerVO;
import com.cskaoyan.service.WxCouponService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author zhuqiushi
 * @since 2022/09/08 16:22
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    /*
     * /wx/coupon/list?page=1&limit=10
     * GET
     * */
    @RequestMapping("list")
    public BaseRespVo<CommonData<WxCouponListInnerVO>> list(Integer page, Integer limit) {
        //调用service层进行查询
        CommonData<WxCouponListInnerVO> wxCouponListVO = wxCouponService.list(page, limit);
        BaseRespVo ok = BaseRespVo.ok(wxCouponListVO);
        return ok;
    }

    /*
     * /wx/coupon/mylist?status=0&page=1&limit=10
     * GET
     * */
    @RequestMapping("mylist")
    public BaseRespVo<CommonData<WxMylistInnerVO>> mylist(Short status, Integer page, Integer limit) {

        //用户id从session中获得
        MarketUser info = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("info");
        Integer userId =  info.getId();
        //调用service查询
        CommonData<WxMylistInnerVO> wxMylistInnerVOCommonData = wxCouponService.mylist(status, page, limit, userId);
        return BaseRespVo.ok(wxMylistInnerVOCommonData);
    }

    /*
     * /wx/coupon/receive
     * post
     * 向用户添加优惠券的方法
     * */
    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody WxCouponReceiveBO wxCouponReceiveBO) {

        MarketUser info = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("info");
        Integer userId =  info.getId();
        int code = wxCouponService.receive(userId, wxCouponReceiveBO.getCouponId());
        BaseRespVo ok = BaseRespVo.ok();
        if (code == 400) {
            ok.setErrno(400);
            ok.setErrmsg("给优惠券已失效");
        }
        return ok;
    }

    /*
     * /wx/coupon/selectlist?cartId=0&grouponRulesId=0
     * GET
     * */
    @RequestMapping("selectlist")
    public BaseRespVo<CommonData<WxCouponSelectlistInnerVO>> selectlist(Integer cartId, Integer grouponRulesId
            , HttpSession session) {
        MarketUser info = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("info");
        Integer userId =  info.getId();
        //调用service层查询
        CommonData<WxCouponSelectlistInnerVO> wxCouponSelectlistCommonData = wxCouponService.selectlist(cartId
                , grouponRulesId, userId);
        BaseRespVo ok = BaseRespVo.ok(wxCouponSelectlistCommonData);
        return ok;
    }

    /*
     * /wx/coupon/exchange
     * POST
     * 742:兑换码不正确，0:成功
     * */
    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody WxCouponExchangeBO wxCouponExchangeBO, HttpSession session) {
        MarketUser info = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("info");
        Integer userId =  info.getId();
        int code = wxCouponService.exchange(wxCouponExchangeBO.getCode(), userId);
        BaseRespVo ok = BaseRespVo.ok();
        if (code == 742) {
            ok.setErrno(code);
            ok.setErrmsg("兑换码不正确，或该优惠券已过期");
        }
        return ok;

    }


}
